package boerse.main.StockPriceProvider;

import boerse.main.Asset.Share;
import boerse.main.Exceptions.NegativeValueException;

/**
 * Change the share price to a random value between -12 and 12. 
 * @author Domi
 *
 */
public class DynamicRandomStockPriceProvider extends StockPriceProvider {

    private double max = 12;
    private double min = -12;

    public DynamicRandomStockPriceProvider() {
        super();
        startUpdate();
    }

    @Override
    protected void updateShareRate(Share share)  {
        long price = share.getPrice();
        if ((price % 50) == 0)
            duUpdateRate(share, (long) (price - (price / 25)));

        price = share.getPrice();
        if(price == 666)
            duUpdateRate(share, 50l);

        price = share.getPrice();
        double x = Math.random()*(max-min+1)+min;
        duUpdateRate(share, price + (long) x);
    }

    private void duUpdateRate(Share share, long price) {
        try {
            share.setPrice(price);
        } catch (NegativeValueException e) {
            return;
        }
    }
}
